package com.chang.manage;

import com.chang.tools.Tools;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

public class Adminstrate {
    HashMap<String,Manager> managers;
    Tools tools;
    Scanner scanner;
    File file;
    public Adminstrate() throws IOException {
        scanner=new Scanner(System.in);
        managers=new HashMap<>();
        tools=new Tools();
        file=new File(tools.File_Path+"\\src\\com\\chang\\manage\\administrate.txt");
        readAdm();
    }
    void readAdm() throws IOException {
        FileReader reader = new FileReader(file);// 低级流
        BufferedReader br = new BufferedReader(reader);
        String str;
        while ((str = br.readLine()) != null) {
            String[] ad_arr=str.split(",");
            Manager manager=new Manager();
            manager.setId(ad_arr[0]);
            manager.setKwd(ad_arr[1]);
            managers.put(ad_arr[0],manager);
        }
    }
    public void logonAdm() throws InterruptedException {
        boolean ip=false;
        String iid;
        Manager manager;
        do{
            System.out.print("请输入id:");
            iid=scanner.next();
            if(managers.get(iid)==null){
                System.out.println("您输入的id有误,请重试!");
                ip=true;
            }else {
                ip=false;
            }
        }while (ip);
        manager=managers.get(iid);
        int count = 5;
        boolean iscontinue = true;
        do {
            System.out.print("请输入您的密码:");
            String kwd = scanner.next();
            if (manager.protectedKey(kwd) ) {
                iscontinue = false;
            } else {
                //可以添加还有几次机会
                count--;
                if (count == 0) {
                    System.out.println("您可以输入的密码次数已经用完，即将退出系统...");
                    Thread.sleep(1000);
                    System.exit(0);
                }
                System.out.println("您的密码输入有误,还剩下" + count + "次机会，请重试!");
                iscontinue = true;
            }
        } while (iscontinue);
    }
}
class Manager{
    String id;
    private String keyword;

    public void setId(String id) {
        this.id = id;
    }
    public void setKwd(String kw){
        keyword=kw;
    }

    public String getId() {
        return id;
    }

    public boolean protectedKey(String st) {
        return st.equals(keyword);
    }
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
}
